package vivek.project.pocketmoney.common.controls
{
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.effects.Resize;
	import mx.effects.Rotate;

	public class MinimizablePanel extends Panel
	{

		public function MinimizablePanel()
		{
			super();
			_minimizer.addEventListener(MouseEvent.CLICK, minimize);
			_hminimizer.setStyle("icon", hMinimizeIcon);
			_minimizer.setStyle("icon", vMinimizeIcon);
			//_hminimizer.addEventListener(MouseEvent.CLICK,hminimize)
		}
		//[Embed("assets/icons/panel_show.gif")]
		public var hMaximizeIcon:Class;
		//[Embed("assets/icons/panel_hide.gif")]
		public var hMinimizeIcon:Class;
		//[Embed("assets/icons/panel_show.gif")]
		public var vMaximizeIcon:Class;
		//[Embed("assets/icons/panel_hide.gif")]
		public var vMinimizeIcon:Class;
		private var _hminimizer:Button=new Button();
		private var _minimizer:Button=new Button();
		private var _oldHeight:int=30;
		private var _oldWidth:int=DEFAULT_MEASURED_MIN_WIDTH;
		private var _oldX:int=x;
		private var _oldY:int=y;
		private var _resizerEffect:Resize=new Resize();
		private var myTextFormat:TextFormat=new TextFormat();
		private var rotate:Rotate=new Rotate(this);

		public function maximizePanel():void
		{
			maximize(null);
		}

		override protected function createChildren():void
		{
			super.createChildren();
			super.titleBar.addChild(_minimizer);
			//super.titleBar.addChild(_hminimizer);
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			var textFormat:TextFormat=new TextFormat();
			textFormat.font="MyMyriad";
			titleTextField.embedFonts=true;
			titleTextField.defaultTextFormat=textFormat;
			_minimizer.x=super.titleBar.width - 55;
			//_hminimizer.x=super.titleBar.width-35;
			_hminimizer.y=2;
			_hminimizer.width=15;
			_hminimizer.height=15;
			_minimizer.y=2;
			_minimizer.width=15;
			_minimizer.height=15;
		}

		private function hmaximize(e:MouseEvent):void
		{
			_hminimizer.setStyle("icon", hMinimizeIcon)
			_hminimizer.removeEventListener(MouseEvent.CLICK, hmaximize)
			_hminimizer.addEventListener(MouseEvent.CLICK, hminimize)
			//rotate.originX=width/2;
			//rotate.originY=height/2;
			rotate.angleFrom=-90;
			rotate.angleTo=0;
			rotate.play();
			y=_oldY;
			height=_oldHeight;
		}

		private function hminimize(e:MouseEvent):void
		{
			_hminimizer.removeEventListener(MouseEvent.CLICK, hminimize)
			_hminimizer.addEventListener(MouseEvent.CLICK, hmaximize)
			_hminimizer.setStyle("icon", hMaximizeIcon)
			rotate.originX=width / 2;
			rotate.originY=height / 2;
			_oldY=y;
			rotate.angleFrom=0;
			rotate.angleTo=-90;
			rotate.play();
			_oldHeight=height;
			y=y + height;
			height=30;
			//super.updateDisplayList(unscaledWidth,unscaledHeight);
		}

		private function maximize(e:MouseEvent):void
		{
			_resizerEffect.stop()
			_minimizer.setStyle("icon", vMinimizeIcon);
			_minimizer.removeEventListener(MouseEvent.CLICK, maximize)
			_minimizer.addEventListener(MouseEvent.CLICK, minimize)
			_resizerEffect.heightFrom=this.height;
			_resizerEffect.heightTo=_oldHeight;
			_oldHeight=this.height;
			_resizerEffect.play([this]);
		}

		private function minimize(e:MouseEvent):void
		{
			_resizerEffect.stop()
			_minimizer.removeEventListener(MouseEvent.CLICK, minimize)
			_minimizer.addEventListener(MouseEvent.CLICK, maximize)
			_minimizer.setStyle("icon", vMaximizeIcon);
			_resizerEffect.heightFrom=this.height;
			_resizerEffect.heightTo=_oldHeight;
			_oldHeight=this.height;
			_resizerEffect.play([this]);
		}
	}
}